package com.swust.servlet;

import com.google.gson.Gson;
import com.swust.dao.Book;
import com.swust.service.LibServiceImpl;
import net.sf.json.JSONArray;
import org.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "SelectBookServlet",urlPatterns = "/SelectBookServlet")
public class SelectBookServlet extends HttpServlet {

    private LibServiceImpl libService = new LibServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Long bookID = Long.valueOf(request.getParameter("bookID"));
        String bookName = request.getParameter("bookName");
        String bookClass = request.getParameter("catgoryName");
        System.out.println(bookClass + bookID + bookName + "now");

        List<Book> bookList = libService.getBooksByCondition(bookID, bookName, bookClass);
        System.out.println(bookList);
        Gson gson = new Gson();
        String books = gson.toJson(bookList);
        System.out.println(books);

        response.getOutputStream().write(books.getBytes("utf-8"));

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
